<template>
  <div class="card-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <span class="search-label">车牌号码：</span>
      <el-input v-model="params.carNumber" clearable placeholder="请输入内容" class="search-main" />
      <span class="search-label">车主姓名：</span>
      <el-input v-model="params.personName" clearable placeholder="请输入内容" class="search-main" />
      <span class="search-label">状态：</span>
      <el-select v-model="params.cardStatus" placeholder="请选择状态">
        <el-option
          v-for="item in list"
          :key="item.id"
          :label="item.name"
          :value="item.id"
        />
      </el-select>
      <el-button type="primary" class="search-btn" @click="doSearch">查询</el-button>
    </div>
    <!-- 新增删除操作区域 -->
    <div class="create-container">
      <!-- 跳转到 添加月卡页面 -->
      <el-button type="primary" @click="$router.push({ path: '/addyueka' })">添加月卡</el-button>
      <el-button @click="delCartList">批量删除</el-button>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table style="width: 100%" :data="cardList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" />
        <el-table-column type="index" label="序号" />
        <el-table-column prop="personName" label="车主名称" />
        <el-table-column prop="personName" label="联系方式" />
        <el-table-column prop="carNumber" label="车牌号码" />
        <el-table-column prop="personName" label="车辆品牌" />
        <el-table-column prop="totalEffectiveDate" label="剩余有效天数" />
        <el-table-column label="状态" prop="cardStatus" :formatter="formatStatus" />
        <el-table-column label="操作" fixed="right" width="180">
          <template #default="{row}">
            <el-button size="mini" type="text">续费</el-button>
            <el-button size="mini" type="text" @click="editsCard(row.id)">查看</el-button>
            <el-button size="mini" type="text" @click="editCard(row.id)">编辑</el-button>
            <el-button size="mini" type="text" @click="delCard(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="page-container">
      <el-pagination
        layout="total, prev, pager, next"
        :total="params.total"
        :current-page="params.page"
        :page-size="params.pageSize"
        @current-change="changePage"
      />
    </div>
    <!-- 添加楼宇 -->
    <el-dialog
      title="添加楼宇"
      width="580px"
    >
      <!-- 表单接口 -->
      <div class="form-container">
        <!-- <el-form ref="addForm" :model="addForm" :rules="addFormRules">
          <el-form-item label="楼宇名称" prop="name">
            <el-input v-model="addForm.name" />
          </el-form-item>
          <el-form-item label="楼宇层数" prop="floors">
            <el-input v-model="addForm.floors" />
          </el-form-item>
          <el-form-item label="在管面积" prop="area">
            <el-input v-model="addForm.area" />
          </el-form-item>
          <el-form-item label="物业费" prop="propertyFeePrice">
            <el-input v-model="addForm.propertyFeePrice" />
          </el-form-item>
        </el-form> -->
      </div>
      <template #footer>
        <el-button size="mini">取 消</el-button>
        <el-button size="mini" type="primary">确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getCardListAPI, delCardAPI, delCardListAPI } from '@/api/card'
export default {
  data() {
    return {
      formatStatus(row) {
        const MAP = {
          0: '可用',
          1: '已过期'
        }
        return MAP[row.cardStatus]
      },
      // 月卡状态
      list: [
        {
          id: null,
          name: '全部'
        },
        {
          id: 0,
          name: '可用'
        },
        {
          id: 1,
          name: '已过期'
        }
      ],
      // selectedStatus: '', // 用于存储选中的状态
      params: {
        page: 1,
        pageSize: 5,
        total: 0,
        carNumber: null, // 车牌号码
        personName: null, // 车主姓名
        cardStatus: null // 状态
      },
      // 月卡列表
      cardList: [],
      // 已选择列表
      selectedCarList: []
    }
  },
  mounted() {
    this.handleEdit()
  },
  methods: {
    async handleEdit() {
      const { data: { rows, total }} = await getCardListAPI(this.params)
      this.params.total = total
      this.cardList = rows
      // console.log(rows)
    },
    changePage(newPage) {
      this.params.page = newPage
      this.handleEdit()
    },
    doSearch() {
      // 调用接口之前把页数参数重置为1
      this.params.page = 1
      // 刷新列表
      this.handleEdit()
      // this.getCardList()
    },
    // 编辑月卡 携带id跳转
    editCard(id) {
      this.$router.push({
        path: '/addyueka',
        query: {
          id
        }
      })
    },
    // 删除逻辑
    async delCard(id) {
      // 发送请求
      await delCardAPI(id)
      // 提示用户
      this.$message.success('删除成功')
      // 刷新列表
      this.handleEdit()
    },
    handleSelectionChange(rowList) {
      console.log(rowList)
      this.selectedCarList = rowList
    },
    // 批量删除
    async delCartList() {
      // 处理id 发送请求
      await delCardListAPI(this.selectedCarList.map(item => item.id))
      this.$message({
        type: 'success',
        message: '删除成功!'
      })
    },
    async editsCard(id) {
      this.$router.push({
        path: '/addyueka',
        query: {
          id
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.card-container {
  padding: 20px;
  background-color: #fff;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, .9);
  padding-bottom: 20px;
  .search-main {
    width: 220px;
    margin-right: 10px;
  }
  .search-btn {
    margin-left: 20px;
  }
}
.create-container {
  margin: 10px 0px;
}
.page-container {
  padding: 4px 0px;
  text-align: right;
}
.form-container {
  padding: 0px 80px;
}
</style>
